package com.common.aop;

import com.common.controller.JsonResult;
import com.common.controller.ResultCode;
import com.common.entity.User;
import com.common.service.UserService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * @classDescription:
 * @author:xiayingjie
 * @createTime:16/8/6
 */
@Aspect
@Component
public class AuthAOP {
    public static final String AUTH_USER = "loginUser";



    @Autowired
    private UserService userService;


    @Pointcut("@annotation(com.common.aop.CheckToken)")
    public void checkToken() {
    }

    @Around("checkToken()")
    public Object auth(ProceedingJoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();

        Class targetClass = null;
        try {
            targetClass = Class.forName(targetName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Method[] methods = targetClass.getMethods();
        String value="";
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == arguments.length) {
                    value = method.getAnnotation(CheckToken.class).value();
                    break;
                }
            }
        }


        String accessToken = request.getParameter(value);

        User user = null;
        if (StringUtils.isNotBlank(accessToken)) {
            user = this.userService.checkAccessTocken(accessToken);
        }
        //如果用户为空，则直接放回json，否则将数据传递到request对象中
        if (user == null) {
            JsonResult js = new JsonResult(ResultCode.TOKEN_INVALID);
            return js;
        } else {
            request.setAttribute(AUTH_USER, user);
            return joinPoint.proceed();
        }
    }


}
